﻿using Kingdee.BOS;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Core.Report.PlugIn;
using Kingdee.BOS.Core.Report.PlugIn.Args;
using Kingdee.BOS.Core.Util;
using Kingdee.BOS.Util;
using Kingdee.BOS.Web.DynamicForm;
using System;
using System.ComponentModel;
using System.Linq;

namespace DM
{
    [HotUpdate]
    [Description("物料库存表过滤")]
    //账表的表单插件必须实现ISysReportViewPlugIn、ISysReportModelPlugIn接口，用空函数实现即可
    public class MaterialInventoryFilter : AbstractDynamicFormPlugIn, ISysReportViewPlugIn, ISysReportModelPlugIn
    {
        public override void ButtonClick(Kingdee.BOS.Core.DynamicForm.PlugIn.Args.ButtonClickEventArgs e)
        {
            //根据查询条件修改过滤参数；过滤参数最终会被传到取数插件SQLReportServicePlugIn中
            if (e.Key.Equals("FBtnSearch", StringComparison.OrdinalIgnoreCase))
            {
                //if(!Model.GetValue("FCollaborationStatus12").Equals(""))
                //{
                //(Model as ISysReportModel).FilterParameter.CustomFilter["FCollaborationStatus1"] = Model.GetValue("FCollaborationStatus12");
                //}
                //if(Model.GetValue("F_UNW_StartDate_qtr")!=null)
                //{
                //(Model as ISysReportModel).FilterParameter.CustomFilter["FStartDate"] = Model.GetValue("F_UNW_StartDate_qtr");
                //}
                //if (Model.GetValue("F_UNW_EndDate_83g") != null)
                //{
                (Model as ISysReportModel).FilterParameter.CustomFilter["FEndDate"] = Model.GetValue("FEndDate");
                //}
                var filter = (Model as ISysReportModel).FilterParameter.CustomFilter;
                var bdDataObj = Model.GetValue("FMATERIALID1") as Kingdee.BOS.Orm.DataEntity.DynamicObject;
                if (bdDataObj != null)
                {
                    filter["FMATERIALID1_Id"] = bdDataObj[0];
                    filter["FMATERIALID1"] = bdDataObj;
                }
                else
                {
                    filter["FMATERIALID1_Id"] = 0;
                    filter["FMATERIALID1"] = null;
                }
                View.Refresh();
            }
            //base.ButtonClick(e);
        }

        public override void AfterBindData(EventArgs e)
        {
            base.AfterBindData(e);

            //查询完成后，默认会将表头上的控件设为只读。因此需重写AfterBindData事件，再将控件设为可编辑，否则无法修改查询条件
            ReportTitles titles = (Model as ISysReportModel).ReportTitles;
            if (titles != null)
            {
                foreach (ReportTitle title in titles)
                {
                    Control ctrl = ((AbstractDynamicWebFormView)this.View).TryGetControl(title.TitleKey);
                    if (ctrl == null)
                    {
                        continue;
                    }
                    string strValue = title.TitleValue;
                    if (title.DataType == SqlStorageType.SqlSmalldatetime)
                    {
                        DateTime dt = Convert.ToDateTime(strValue.Trim());
                        strValue = FieldFormatterUtil.GetDateFormatString(this.Context, this.Context.CurrentUserTimeZone.ConvertTime(dt, this.Context.SystemTimeZone));
                    }
                    else if (title.DataType == SqlStorageType.SqlDatetime)
                    {
                        DateTime dt = Convert.ToDateTime(strValue.Trim());
                        strValue = FieldFormatterUtil.GetDateTimeFormatString(this.Context, this.Context.CurrentUserTimeZone.ConvertTime(dt, this.Context.SystemTimeZone));
                    }
                    //只读的表头控件设为可编辑
                    ctrl.Enabled = true;
                    //过滤框中的查询条件同步到表头上
                    this.Model.SetValue(title.TitleKey, strValue);
                }
                var wlTitle = titles.FirstOrDefault(x => x.TitleKey == "FMATERIALID1");
                if (wlTitle == null)
                    return;
                Control ctrl1 = ((AbstractDynamicWebFormView)this.View).TryGetControl("FMATERIALID1");
                string strValue1 = wlTitle.TitleValue;
                //只读的表头控件设为可编辑
                ctrl1.Enabled = true;
                //过滤框中的查询条件同步到表头上, 基础资料处理逻辑
                View.Model.SetItemValueByID("FMATERIALID1", strValue1, 0);
                View.UpdateView("FMATERIALID1");
            }
        }

        public void AfterButtonClick(Kingdee.BOS.Core.Report.PlugIn.Args.MenuArgs Args)
        {

        }

        public void AfterGetData()
        {

        }

        public void BeforeButtonClick(Kingdee.BOS.Core.Report.PlugIn.Args.MenuArgs Args)
        {

        }

        public void CellClick(Kingdee.BOS.Core.Report.PlugIn.Args.CellEventArgs Args)
        {

        }

        public void CellDbClick(Kingdee.BOS.Core.Report.PlugIn.Args.CellEventArgs Args)
        {

        }

        public void FormatCellValue(Kingdee.BOS.Core.Report.PlugIn.Args.FormatCellValueArgs args)
        {

        }

        public void ReportInitialize(Kingdee.BOS.Core.Report.PlugIn.Args.ReportInitializeEventArgs e)
        {

        }

        public void OnFormatRowConditions(Kingdee.BOS.Core.Report.PlugIn.Args.ReportFormatConditionArgs args)
        {

        }

        public void CreateFilterEditorControl(Kingdee.BOS.Core.DynamicForm.PlugIn.Args.CreateControlEventArgs e)
        {
        }

        public void PrepareFilterParameter(Kingdee.BOS.Core.Report.PlugIn.Args.FilterArgs e)
        {
        }

        public void AfterGetAllPageDataSet(AfterGetAllPageDataSetEventArgs e)
        {
            throw new NotImplementedException();
        }

        public void BeforeBuildExcelSheet(BeforeBuildExcelSheetEventArgs e)
        {
            throw new NotImplementedException();
        }

        public void AfterBuildExportReportTitle(AfterBuildExportReportTitleEventArgs e)
        {
            throw new NotImplementedException();
        }
    }
}